package myJs.user.soft.preprocess

import com.softwaremill.quicklens._
import myJs.cps._
import myJs.myPkg.reactHookForm._
import myJs.tool.Tool
import myJs.tool.Tool._
import myJs.user.soft.preprocess.AllCp.PreprocessT
import shared.Pojo._
import shared.pojo.WorkflowMission._

import scala.collection.SeqMap

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.Utils._

/** Created by yz on 3/11/2020
  */
object NormalCp extends PreprocessT with NormalSoftT {

  case class Props(
      curI: Int,
      datasChange: List[EachPreprocessData] => SyncIO[Unit],
      datas: List[EachPreprocessData],
      insertModule: String => SyncIO[Unit],
      removeModule: Int => SyncIO[Unit]
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    object FTool {

      def normalMethodChange = (v: String) => {
        val newPreProcess = props.datas.modify(_.index(props.curI).normalMethod).setTo(v)
        props.datasChange(newPreProcess)
      }

      def areaChange = (v: String) => {
        val newPreProcess = props.datas.modify(_.index(props.curI).area).setTo(v)
        props.datasChange(newPreProcess)
      }

      def removeModuleF =  {
        props.removeModule(props.curI)
      }

      def insertModule = (moduleName: String) => {
        props.insertModule(moduleName)
      }

    }

    useEffect(
      SyncIO{
        Tool.popover
      }.void,
      List()
    )

    val normalMethodMap = SeqMap("sum" -> messages("sum"), "median" -> messages("median"))
    val areaMap =
      SeqMap("1" -> "1", "10000" -> messages("tenThousand"), "1000000" -> messages("aMillion"), "mean" -> "mean")
    val areaQuestionInfo = messages("levelToInfo")
    val data = props.datas(props.curI)
    val normalMethod = data.normalMethod
    val area = data.area
    val namePrefix = s"datas.${props.curI}"

    fieldset(
      paddingRight := "5px", border := "1px solid silver", margin := "0 2px", borderRadius := "4px",
      className := "northbtn myEachSoft",
      MyToolCp.Props(removeModule = FTool.removeModuleF, insertModule = FTool.insertModule).render,
      legend(s"${messages(nameStr)}"),
      div(
        FormGroupInlineCp(names = List.empty)(
          SelectInlineNoFormGroupCp.Props(
            value = normalMethod,
            label = s"${messages("dataNormalizationMethod")}",
            name = s"${namePrefix}.normalMethod",
            optionMap = normalMethodMap,
            onChange = FTool.normalMethodChange
          ).render,
          SelectInlineNoFormGroupCp.Props(
            value = area,
            label = messages("levelTo"),
            name = s"${namePrefix}.area",
            show = (normalMethod == "sum"),
            optionMap = areaMap,
            onChange = FTool.areaChange,
            questionInfo = areaQuestionInfo
          ).render
        )
      )
    )

  }

  def cp(
      curI: Int,
      datas: List[EachPreprocessData],
      datasChange: List[EachPreprocessData] => SyncIO[Unit],
      insertModule: String => SyncIO[Unit],
      removeModule: Int => SyncIO[Unit],
      extraData: ExtraData
  ) = {
    NormalCp.Props(
      curI = curI,
      datas = datas,
      datasChange = datasChange,
      insertModule = insertModule,
      removeModule = removeModule
    ).render
  }

}
